package com.meilingcloud.biobank.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meilingcloud.biobank.domain.Family;
import com.meilingcloud.biobank.domain.FamilySource;
import com.meilingcloud.biobank.mapper.FamilyMapper;
import com.meilingcloud.biobank.service.IFamilyService;
import com.meilingcloud.biobank.service.IFamilySourceService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 家系 Service业务层处理
 *
 * @author chenl
 * @date 2021-06-22
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@Service
public class FamilyServiceImpl extends ServiceImpl<FamilyMapper, Family> implements IFamilyService {

    private final IFamilySourceService familySourceService;

    @Override
    public List<Family> queryList(Family family) {
        LambdaQueryWrapper<Family> lqw = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(family.getFamilyName())) {
            lqw.like(Family::getFamilyName, family.getFamilyName());
        }
        if (StringUtils.isNotBlank(family.getFamilyClinical())) {
            lqw.eq(Family::getFamilyClinical, family.getFamilyClinical());
        }
        return this.list(lqw);
    }

    @Override
    public FamilySource getFamilyMap(Long familyId) {
        List<FamilySource> list = familySourceService.list(new LambdaQueryWrapper<FamilySource>().eq(FamilySource::getFamilyId, familyId));
        FamilySource pSource = findParent(list);
        return findChildAndMate(pSource, list);
    }

    /**
     * 获取根节点
     */
    private FamilySource findParent(List<FamilySource> list) {
        FamilySource familySource = new FamilySource();
        for (FamilySource source : list) {
            if (ObjectUtil.isNotNull(source.getFamilySourcePid()) && source.getFamilySourcePid() == 0L) {
                familySource = source;
            }
        }
        return familySource;
    }

    /**
     * 获取子节点
     */
    private FamilySource findChildAndMate(FamilySource pSource, List<FamilySource> list) {
        for (FamilySource fs : list) {
            if (ObjectUtil.equal(fs.getFamilySourceId(), pSource.getFamilySourceMid())) {
                if (null == pSource.getMate()) {
                    pSource.setMate(new ArrayList<>());
                }
                pSource.getMate().add(fs);
            }
            if (ObjectUtil.equal(fs.getFamilySourcePid(), pSource.getFamilySourceId())) {
                if (null == pSource.getChildren()) {
                    pSource.setChildren(new ArrayList<>());
                }
                pSource.getChildren().add(findChildAndMate(fs, list));
            }
        }
        return pSource;
    }

}
